package com.mrtop.jigsawapp;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class Game extends Activity {

	private TextView moveCounter;
	private TextView feedbackText;
	private Button[] buttons;
	private Boolean bad_move = false;
	private Button showhidebtn;
	private static final Integer[] goal = new Integer[] { 0, 1, 2, 3, 4, 5, 6,
			7, 8 };

	private ArrayList<Integer> cells = new ArrayList<Integer>();
	int isShow = 0;
	String levelInfo="";
	String level = "";
	String moves = "";
	String nextLevel="";
	private PopupWindow popUp;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game);
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		Button btnBack = (Button)this.findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getBaseContext(), Levels.class);
				finish();
				startActivity(intent);
			}
		});
//get extras sent from other layout		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
		    levelInfo = extras.getString("LEVEL");
		    int temp = levelInfo.indexOf(":");
		    moves = levelInfo.substring(temp+1);
		    level = levelInfo.substring(0, temp);
		    int l = Integer.parseInt(level) + 1;
		    int m = Integer.parseInt(moves) - 5;
		    nextLevel = l + ":" + m;
		}
		
		Button btnReplay = (Button)this.findViewById(R.id.btnReplay);
		btnReplay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getBaseContext(), Game.class);
				intent.putExtra("LEVEL", levelInfo);
				finish();
				startActivity(intent);
			}
		});
		buttons = findButtons();
		showhidebtn = (Button)findViewById(R.id.showhideButton);
		showhidebtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isShow==0) {
					for(int i = 0; i<9; i++) {
						buttons[i].setTextSize(1);						
					}
					showhidebtn.setText("Show");
					isShow = 1;
				} else {
					for(int i = 0; i<9; i++) {
						buttons[i].setTextSize(28);					
					}
					showhidebtn.setText("Hide");
					isShow = 0;
				}
			}
		});

		for (int i = 0; i < 9; i++) {
			this.cells.add(i);
		}
		Collections.shuffle(this.cells); // random cells array

		fill_grid();

		moveCounter = (TextView) findViewById(R.id.MoveCounter);
		feedbackText = (TextView) findViewById(R.id.FeedbackText);

		for (int i = 1; i < 9; i++) {
			buttons[i].setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					makeMove((Button) v);
				}
			});
		}

		moveCounter.setText(moves);
		//feedbackText.setText(R.string.game_feedback_text);

	}

	public Button[] findButtons() {
		Button[] b = new Button[9];

		b[0] = (Button) findViewById(R.id.Button00);
		b[1] = (Button) findViewById(R.id.Button01);
		b[2] = (Button) findViewById(R.id.Button02);
		b[3] = (Button) findViewById(R.id.Button03);
		b[4] = (Button) findViewById(R.id.Button04);
		b[5] = (Button) findViewById(R.id.Button05);
		b[6] = (Button) findViewById(R.id.Button06);
		b[7] = (Button) findViewById(R.id.Button07);
		b[8] = (Button) findViewById(R.id.Button08);
		return b;
	}

	public void makeMove(final Button b) {
		bad_move = true;
		int b_text, b_pos, zuk_pos;
		b_text = Integer.parseInt((String) b.getText());
		b_pos = find_pos(b_text);
		zuk_pos = find_pos(0);
		switch (zuk_pos) {
		case (0):
			if (b_pos == 1 || b_pos == 3)
				bad_move = false;
			break;
		case (1):
			if (b_pos == 0 || b_pos == 2 || b_pos == 4)
				bad_move = false;
			break;
		case (2):
			if (b_pos == 1 || b_pos == 5)
				bad_move = false;
			break;
		case (3):
			if (b_pos == 0 || b_pos == 4 || b_pos == 6)
				bad_move = false;
			break;
		case (4):
			if (b_pos == 1 || b_pos == 3 || b_pos == 5 || b_pos == 7)
				bad_move = false;
			break;
		case (5):
			if (b_pos == 2 || b_pos == 4 || b_pos == 8)
				bad_move = false;
			break;
		case (6):
			if (b_pos == 3 || b_pos == 7)
				bad_move = false;
			break;
		case (7):
			if (b_pos == 4 || b_pos == 6 || b_pos == 8)
				bad_move = false;
			break;
		case (8):
			if (b_pos == 5 || b_pos == 7)
				bad_move = false;
			break;
		}

		if (bad_move == true) {
			//feedbackText.setText("Move Not Allowed");
			return;
		}
		//feedbackText.setText("Move OK");
		cells.remove(b_pos);
		cells.add(b_pos, 0);
		cells.remove(zuk_pos);
		cells.add(zuk_pos, b_text);

		fill_grid();
		moveCounter.setText(Integer.toString(Integer
				.parseInt((String) moveCounter.getText()) - 1));

		if(moveCounter.getText().equals("0")) {
			int[] location = new int[2]; 
			Point p = new Point();
			   p.x = location[0];
			   p.y = location[1];
			
			RelativeLayout viewGroup = (RelativeLayout) Game.this.findViewById(R.id.popup1);
			   LayoutInflater layoutInflater = (LayoutInflater) Game.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			   View layout = layoutInflater.inflate(R.layout.faillayout, viewGroup);
			   
			   popUp = new PopupWindow(Game.this);
			   popUp.setContentView(layout);
			   popUp.setWidth(300);
			   popUp.setHeight(250);
			   popUp.setFocusable(true);
			   
			   int OFFSET_X = 10;
			   int OFFSET_Y = 150;
			   
			   popUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
			   
			   final MediaPlayer mp = MediaPlayer.create(this, R.raw.fail);
			   mp.start();
			   Button btnAgain = (Button)layout.findViewById(R.id.btnAgain);
			   btnAgain.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					mp.stop();
					popUp.dismiss();
					Intent intent = new Intent(getBaseContext(), Game.class);
					intent.putExtra("LEVEL",levelInfo);
					finish();
					startActivity(intent);					
				}
			});
			   
			   Button btnExit = (Button)layout.findViewById(R.id.btnExit);
			   btnExit.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					mp.stop();
					popUp.dismiss();
					Intent intent = new Intent(getBaseContext(), Levels.class);
					//intent.putExtra("STAR",levelInfo);
					finish();
					startActivity(intent);
				}
			});

		}
		
		for (int i = 0; i < 9; i++) {
			if (cells.get(i) != goal[i]) {
				return;
			}
		}
		
		//feedbackText.setText("we have a winner");
		int[] location = new int[2]; 
		Point p = new Point();
		   p.x = location[0];
		   p.y = location[1];
		
		RelativeLayout viewGroup = (RelativeLayout) Game.this.findViewById(R.id.popup);
		   LayoutInflater layoutInflater = (LayoutInflater) Game.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		   View layout = layoutInflater.inflate(R.layout.winlayout, viewGroup);
		   
		   popUp = new PopupWindow(Game.this);
		   popUp.setContentView(layout);
		   popUp.setWidth(300);
		   popUp.setHeight(250);
		   popUp.setFocusable(true);
		   
		   int OFFSET_X = 10;
		   int OFFSET_Y = 150;
		   
		   popUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
		   
		   final MediaPlayer mp = MediaPlayer.create(this, R.raw.win);
		   mp.start();
		   
		   Button btnAgain = (Button)layout.findViewById(R.id.btnAgain);
		   btnAgain.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mp.stop();
				popUp.dismiss();
				Intent intent = new Intent(getBaseContext(), Game.class);
				intent.putExtra("LEVEL",levelInfo);
				finish();
				startActivity(intent);					
			}
		});
		   
		   Button btnNext = (Button)layout.findViewById(R.id.btnNext);
		   btnNext.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(level.equals("6")) {
					mp.stop();
					popUp.dismiss();
					Intent intent = new Intent(getBaseContext(), Levels.class);
					//intent.putExtra("STAR",levelInfo);
					finish();
					startActivity(intent);
				} else if(!level.equals("6")) {
					mp.stop();
					popUp.dismiss();
					Intent intent = new Intent(getBaseContext(), Game.class);
					intent.putExtra("LEVEL",nextLevel);
					finish();
					startActivity(intent);
				}
			}
		});
		   
		   Button btnExit = (Button)layout.findViewById(R.id.btnExit);
		   btnExit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mp.stop();
				popUp.dismiss();
				Intent intent = new Intent(getBaseContext(), Levels.class);
				//intent.putExtra("STAR",levelInfo);
				finish();
				startActivity(intent);
			}
		});
		   
		
		for(int i = 0; i<9; i++) {
			buttons[i].setTextSize(1);
		}	
		showhidebtn.setText("Show");
		showhidebtn.setClickable(false);
		buttons[0].setBackgroundResource(R.drawable.a);
	}

	public void fill_grid() {
		for (int i = 0; i < 9; i++) {
			int text = cells.get(i);
			AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) buttons[text]
					.getLayoutParams();
			switch (i) {
			case (0):

				absParams.x = 7;
				absParams.y = 7;
				buttons[text].setLayoutParams(absParams);
				break;
			case (1):

				absParams.x = 107;
				absParams.y = 7;
				buttons[text].setLayoutParams(absParams);
				break;
			case (2):

				absParams.x = 207;
				absParams.y = 7;
				buttons[text].setLayoutParams(absParams);
				break;
			case (3):

				absParams.x = 7;
				absParams.y = 107;
				buttons[text].setLayoutParams(absParams);
				break;
			case (4):

				absParams.x = 107;
				absParams.y = 107;
				buttons[text].setLayoutParams(absParams);
				break;
			case (5):

				absParams.x = 207;
				absParams.y = 107;
				buttons[text].setLayoutParams(absParams);
				break;
			case (6):

				absParams.x = 7;
				absParams.y = 207;
				buttons[text].setLayoutParams(absParams);
				break;
			case (7):

				absParams.x = 107;
				absParams.y = 207;
				buttons[text].setLayoutParams(absParams);
				break;
			case (8):

				absParams.x = 207;
				absParams.y = 207;
				buttons[text].setLayoutParams(absParams);
				break;

			}

		}

	}

	public int find_pos(int element) {
		int i = 0;
		for (i = 0; i < 9; i++) {
			if (cells.get(i) == element) {
				break;
			}
		}
		return i;
	}
	
	public void onBackPressed() {
		Intent myIntent = new Intent(Game.this, Main.class);
		myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(myIntent);
		finish();
		return;
	}
}
